package org.wu.framework.ssh.server.platform.controller;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.http.ResponseEntity;
import org.wu.framework.ssh.server.platform.application.command.ssh.server.config.*;
import org.wu.framework.ssh.server.platform.entity.ServerConfig;
import org.wu.framework.web.response.ResultFactory;
import org.wu.framework.web.spring.EasyController;
import org.springframework.web.bind.annotation.*;
import org.wu.framework.web.response.Result;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.Map;

import org.wu.framework.ssh.server.platform.domain.model.ssh.server.config.SshServerConfig;
import org.wu.framework.ssh.server.platform.application.SshServerConfigApplication;
import org.wu.framework.ssh.server.platform.application.dto.SshServerConfigDTO;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.easy.excel.stereotype.RequestExcelBody;
import org.wu.framework.easy.excel.stereotype.EasyExcel;
/**
 * describe ssh服务器信息 
 *
 * @author Jia wei Wu
 * @date 2025/09/13 10:07 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyController 
 **/
@Tag(name = "ssh服务器信息提供者")
@EasyController("/ssh/server/config")
public class SshServerConfigProvider  {

    @Resource
    private SshServerConfigApplication sshServerConfigApplication;

    /**
     * describe 新增ssh服务器信息
     *
     * @param sshServerConfigStoryCommand 新增ssh服务器信息     
     * @return {@link Result<SshServerConfig>} ssh服务器信息新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "新增ssh服务器信息")
    @PostMapping("/story")
    public Result<SshServerConfig> story(@RequestBody SshServerConfigStoryCommand sshServerConfigStoryCommand){
        return sshServerConfigApplication.story(sshServerConfigStoryCommand);
    }
    /**
     * describe 批量新增ssh服务器信息
     *
     * @param sshServerConfigStoryCommandList 批量新增ssh服务器信息     
     * @return {@link Result<List<SshServerConfig>>} ssh服务器信息新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "批量新增ssh服务器信息")
    @PostMapping("/batchStory")
    public Result<List<SshServerConfig>> batchStory(@RequestBody List<SshServerConfigStoryCommand> sshServerConfigStoryCommandList){
        return sshServerConfigApplication.batchStory(sshServerConfigStoryCommandList);
    }
    /**
     * describe 更新ssh服务器信息
     *
     * @param sshServerConfigUpdateCommand 更新ssh服务器信息     
     * @return {@link Result<SshServerConfig>} ssh服务器信息领域对象     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "更新ssh服务器信息")
    @PutMapping("/updateOne")
    public Result<SshServerConfig> updateOne(@RequestBody SshServerConfigUpdateCommand sshServerConfigUpdateCommand){
        return sshServerConfigApplication.updateOne(sshServerConfigUpdateCommand);
    }
    /**
     * describe 查询单个ssh服务器信息
     *
     * @param sshServerConfigQueryOneCommand 查询单个ssh服务器信息     
     * @return {@link Result<SshServerConfigDTO>} ssh服务器信息DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "查询单个ssh服务器信息")
    @GetMapping("/findOne")
    public Result<SshServerConfigDTO> findOne(@ModelAttribute SshServerConfigQueryOneCommand sshServerConfigQueryOneCommand){
        return sshServerConfigApplication.findOne(sshServerConfigQueryOneCommand);
    }
    /**
     * describe 查询单个ssh服务器信息
     *
     * @param id 查询单个ssh服务器信息
     * @return {@link Result<SshServerConfigDTO>} ssh服务器信息DTO对象

     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "查询单个ssh服务器信息")
    @GetMapping("/findOneById/{id}")
    public Result<SshServerConfigDTO> findOneById(@PathVariable Integer id){
        return sshServerConfigApplication.findOneById(id);
    }
    /**
     * describe 查询多个ssh服务器信息
     *
     * @param sshServerConfigQueryListCommand 查询多个ssh服务器信息     
     * @return {@link Result<List<SshServerConfigDTO>>} ssh服务器信息DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "查询多个ssh服务器信息")
    @GetMapping("/findList")
    public Result<List<SshServerConfigDTO>> findList(@ModelAttribute SshServerConfigQueryListCommand sshServerConfigQueryListCommand){
        return sshServerConfigApplication.findList(sshServerConfigQueryListCommand);
    }
    /**
     * describe 分页查询多个ssh服务器信息
     *
     * @param sshServerConfigQueryListCommand 分页查询多个ssh服务器信息     
     * @return {@link Result<LazyPage<SshServerConfigDTO>>} 分页ssh服务器信息DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "分页查询多个ssh服务器信息")
    @GetMapping("/findPage")
    public Result<LazyPage<SshServerConfigDTO>> findPage(@Parameter(description ="分页大小") @RequestParam(defaultValue = "10", value = "size") int size,
                           @Parameter(description ="当前页数") @RequestParam(defaultValue = "1", value = "current") int current,@ModelAttribute SshServerConfigQueryListCommand sshServerConfigQueryListCommand){
        return sshServerConfigApplication.findPage(size,current,sshServerConfigQueryListCommand);
    }
    /**
     * describe 删除ssh服务器信息
     *
     * @param sshServerConfigRemoveCommand 删除ssh服务器信息     
     * @return {@link Result<SshServerConfig>} ssh服务器信息     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "删除ssh服务器信息")
    @DeleteMapping("/remove")
    public Result<SshServerConfig> remove(@ModelAttribute SshServerConfigRemoveCommand sshServerConfigRemoveCommand){
        return sshServerConfigApplication.remove(sshServerConfigRemoveCommand);
    }
    /**
     * describe 导出ssh服务器信息
     *
     * @param sshServerConfigQueryListCommand 导出ssh服务器信息     
     * @return {@link List< SshServerConfigExcelExport >} ssh服务器信息
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @EasyExcel(fileName = "导出ssh服务器信息数据")
    @Operation(summary = "导出ssh服务器信息")
    @GetMapping("/exportExcel")
    public List<SshServerConfigExcelExport> exportExcel(@ModelAttribute SshServerConfigQueryListCommand sshServerConfigQueryListCommand){
        return sshServerConfigApplication.exportExcel(sshServerConfigQueryListCommand);
    }
    /**
     * describe 导出模版ssh服务器信息
     *
     
     * @return {@link Result<List<SshServerConfigExcelExport>>} ssh服务器信息     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @EasyExcel(fileName = "导出ssh服务器信息模版数据")
    @Operation(summary = "导出ssh服务器信息模版")
    @GetMapping("/exportTemplateExcel")
    public List<SshServerConfigExcelExport> exportTemplateExcel(){
        return List.of(new SshServerConfigExcelExport());
    }
    /**
     * describe 导入ssh服务器信息
     *
     * @param sshServerConfigExcelImportList 导入ssh服务器信息     
     * @return {@link Result<SshServerConfigExcelImport>} ssh服务器信息     
     
     * @author Jia wei Wu
     * @date 2025/09/13 10:07 晚上
     **/

    @Operation(summary = "导入ssh服务器信息")
    @PostMapping("/importExcel")
    public Result<List<SshServerConfigExcelImport>> importExcel(@RequestExcelBody("multipartFile") List<SshServerConfigExcelImport> sshServerConfigExcelImportList){
        return sshServerConfigApplication.importExcel(sshServerConfigExcelImportList);
    }
    /**
     * 测试服务器连接
     */
    @PostMapping("/test")
    public Result<Map<String, Object>> testConnection(@RequestBody ServerConfig server) {
        try {
            // 验证必要参数
            if (server.getHost() == null || server.getHost().trim().isEmpty()) {
                return ResultFactory.errorOf(Map.of("success", false, "message", "服务器地址不能为空"));
            }
            if (server.getUsername() == null || server.getUsername().trim().isEmpty()) {
                return ResultFactory.errorOf(Map.of("success", false, "message", "用户名不能为空"));
            }
            if (server.getPassword() == null || server.getPassword().trim().isEmpty()) {
                return ResultFactory.errorOf(Map.of("success", false, "message", "密码不能为空"));
            }

            // 设置默认端口
            int port = server.getPort() != null ? server.getPort() : 22;

            // 简单的连接测试
            JSch jsch = new JSch();
            Session session = jsch.getSession(server.getUsername(), server.getHost(), port);
            session.setPassword(server.getPassword());
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect(5000); // 5秒超时
            session.disconnect();

            return ResultFactory.successOf(Map.of("success", true, "message", "连接测试成功"));
        } catch (Exception e) {
            return ResultFactory.errorOf(Map.of("success", false, "message", "连接测试失败: " + e.getMessage()));
        }
    }

}